package com.huike.web.controller.business;

import cn.hutool.core.bean.BeanUtil;
import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商机跟进记录Controller
 * @date 2021-04-28
 */
@RestController
@RequestMapping("/business/record")
public class TbBusinessTrackRecordController extends BaseController {

    @Autowired
    private ITbBusinessTrackRecordService tbBusinessTrackRecordService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询商机跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:record:list')")
    @GetMapping("/list")
    public  AjaxResult list(@RequestParam("businessId")Long id){//1.接收请求参数
//        2.调用service查询
        List<TbBusinessTrackRecord>tbBusinessTrackRecordList=tbBusinessTrackRecordService.findListByBusinessId(id);
//        3.返回结果
        return AjaxResult.success(tbBusinessTrackRecordList);
    }

    /**
     * 新增商机跟进记录
     */
    @PreAuthorize("@ss.hasPermi('business:record:add')")
    @Log(title = "商机跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTrackVo businessTrackVo){
//        1.创建商机跟进对象
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
//        1.2复制参数属性
        BeanUtil.copyProperties(businessTrackVo,tbBusinessTrackRecord);
//        1.3设置跟进对象的创建人创建时间
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
//        2.创建商机对象
        TbBusiness tbBusiness = new TbBusiness();
//        2.1复制参数属性
        BeanUtil.copyProperties(businessTrackVo,tbBusiness);
        tbBusiness.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());//修改商机状态
        tbBusiness.setId(businessTrackVo.getBusinessId());//获取参数的商机id,mapper层可用其作为条件
//        3.调用service层
tbBusinessTrackRecordService.insert(tbBusinessTrackRecord,tbBusiness);
//4.返回结果
        return AjaxResult.success();
    }
}